//
//  NewWishTableViewCell.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "NewWishTableViewCell.h"

@interface NewWishTableViewCell ()

@property (nonatomic, weak)UIImageView * contentImageView;
@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation NewWishTableViewCell


- (void)setModel:(HopeModel *)model{
    
    _model = model;
    
    self.contentLabel.text = model.title;
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd"];
    NSString *dateString       = [formatter stringFromDate: date];
    
    self.timeLabel.text = dateString;
    
    NSArray * arr = model.images;
    
    if (arr.count > 0) {
        
        NSURL * url = [NSURL URLWithString:arr.firstObject];
        [self.contentImageView sd_setImageWithURL:url];
        
        [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.timeLabel);
            make.leading.mas_equalTo(self.contentView).offset(120 * SCREENH6);
            make.width.mas_equalTo(85 * SCREENH6);
            make.height.mas_equalTo(55 * SCREENH6);
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(self.contentImageView.mas_trailing).offset(2 * SCREENH6);
            make.trailing.mas_equalTo(self.contentView).offset(-8 * SCREENH6);
            make.top.mas_equalTo(self.contentImageView);
        }];
    }else{
        
        self.contentView.hidden = YES;
        
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.timeLabel);
            make.leading.mas_equalTo(self.contentView).offset(120 * SCREENH6);
            make.trailing.mas_equalTo(self.contentView).offset(-8 * SCREENH6);
        }];
    }
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}



- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = BLACK_COLOR;
    timeLabel.font = [UIFont systemFontOfSize:25 * SCREENH6];
    timeLabel.text = @"8月20";
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
    
    
    UIImageView * contentImageView = [UIImageView new];
    
    contentImageView.backgroundColor = [UIColor lightGrayColor];
    
    self.contentImageView = contentImageView;
    
    [self.contentView addSubview:self.contentImageView];

    
    
    UILabel * contentLabel = [UILabel new];
    
    contentLabel.textColor = BLACK_COLOR;
    contentLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    contentLabel.numberOfLines = 3;
    contentLabel.text = @"color on UITableViewHeaderFooterView has been deprecated. Please set a custom UIView with your desired background color to the backgroundView property instead.";
    
    self.contentLabel = contentLabel;
    
    [self.contentView addSubview:self.contentLabel];

    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);//[UIColor lightGrayColor];
    
    self.lineView = lineView;
    
    [self.contentView addSubview:self.lineView];
    
}

- (void)layoutSubviews{
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(12 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(20 * SCREENH6);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLabel);
        make.leading.mas_equalTo(self.contentView).offset(120 * SCREENH6);
        make.width.mas_equalTo(85 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentImageView.mas_trailing).offset(2 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-8 * SCREENH6);
        make.top.mas_equalTo(self.contentImageView);
    }];

    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
        make.bottom.mas_equalTo(self.contentView);
    }];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
